﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.Xml.Linq;

namespace GameTank
{
    public class Construction : VisibleGameEntity
    {
        ConstructionDTO detail;
        Texture2D texture;

        public Construction(ConstructionDTO NewDetail, ContentManager Content)
        {                     
            detail = NewDetail;
            XDocument doc = XDocument.Load(detail.FilePathMD);
            string filePath = ModelDTO.Parse(doc.Root).FilePath;
            texture = Content.Load<Texture2D>(filePath);
            // TODO: Construct any child components here
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
           
        
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch sBatch)
        {
            // Draw the ship
            // spriteRectangle,
            float h = (1-detail.CurrentHp / detail.Hp);
            int k = (int)(50*h);
            k %= 255;
            sBatch.Begin();
            sBatch.Draw(texture, detail.Pos, Color.FromNonPremultiplied(255 - k, 255 - k, 255 - k, 255));
            sBatch.End();
        }

        public void remove()
        {
            detail.Pos = new Vector2(-64, -64);
        }
        
        public Rectangle bounder
        {
            get { return new Rectangle((int)detail.Pos.X, (int)detail.Pos.Y, texture.Width, texture.Height); }
            set { }
        }

        public bool isBroken()
        {
            return (detail.Broken);
        }

        public void beAttack(float damage)
        {
            this.detail.CurrentHp -= damage;
            if (this.detail.Broken)
                this.remove();
        }

        public ConstructionDTO save
        {
            get { return detail; }
        }
        // end IConstruction
    }
}
